const util = require("../../../utils/util");

// pages/business/price-template/price-template.js
var app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    priceLists:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  // 添加价格模板
  addPrice(){
    this.data.priceLists.push({
      name:'模板',
      price_rto:"0",
      show_price_rto:"0",
      edit:true
    })
    this.setData({
      priceLists:this.data.priceLists
    })
  },
  // input框失去焦点
  inputBlur(e){
    let dataset = e.currentTarget.dataset,value = e.detail.value;
    dataset.item[dataset.type] = value;
    console.log("拼团价格",value,dataset.item);
    this.setData({
      ['priceLists['+dataset.index+']']:dataset.item
    })
  },
  // 删除
  delete(e){
    wx.showLoading({
      title:"正在删除..."
    })
    var that = this,id=e.currentTarget.dataset.item.id;
    wx.request({
      url: app.globalData.urls + '/product/price-template-del',
      method: 'POST',
      data: {
        bdmin_user_id:util.getBdmin_user_id(),
        id:id
      },
      header: app.getPostRequestHeader(),
      success: function (res) {
        wx.hideLoading({
          title:"删除成功"
        })
        if (res.data.code == '200') {
          wx.showToast({
            title: '删除成功',
            icon:'none'
          })
          that.getPriceTemplate();
        }else{
          wx.showToast({
            title: '删除失败',
            icon:'none'
          })
        }
      }
    })
  },
  // 编辑
  edit(e){
    let dataset = e.currentTarget.dataset;
    dataset.item.edit = true;
    this.setData({
      ['priceLists['+dataset.index+']']:dataset.item
    })
    console.log(this.data.priceLists)
  },
  // 保存
  save(e){
    let dataset = e.currentTarget.dataset,item = dataset.item;
    var that = this,url = item.id ? '/product/price-template-update' :'/product/price-template-add';
    console.log("保存")
    wx.request({
      url: app.globalData.urls + url,
      method: 'POST',
      data: {
        bdmin_user_id:util.getBdmin_user_id(),
        name:item.name,
        price_rto:item.price_rto,
        show_price_rto:item.show_price_rto,
        id:item.id
      },
      header: app.getPostRequestHeader(),
      success: function (res) {
        if (res.data.code == '200') {
          item.edit = false
          that.setData({
            ['priceLists['+dataset.index+']']:item
          })
          if(!item.id) that.getPriceTemplate();
        }else{
          wx.showToast({
            title: '保存失败',
            icon:'none'
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getPriceTemplate()
  },
  // 获取价格模板列表
  getPriceTemplate(){
    wx.showLoading()
    var that=this;
    wx.request({
      url: app.globalData.urls + '/product/price-template',
      method: 'GET',
      data: {
        bdmin_user_id:util.getBdmin_user_id()
      },
      header: app.getPostRequestHeader(),
      success: function (res) {
        wx.hideLoading()
        if(res.data.code==200){
          let data = res.data.data;
          that.setData({
            priceLists:data
          })
        }
      }
    })
  },
})